<template>
  <div>
    <!-- 索赔发票审核 -- 车厂-->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      columnsRef="ClaimInvoiceChecklgr"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary"  :click="exportFileBtn.bind()">导出</multiuse-button>
        <multiuse-button size="small" type="primary"  :click="batchExamine.bind()">批量审核</multiuse-button>
<!--        <multiuse-button size="small" type="primary"  :click="batchReject.bind()">批量驳回</multiuse-button>-->
      </template>
    </FilterTable>
    <Drawer :visible.sync="visible" :title="'发票信息审核'" width="60%" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <xiangqing @close="close" @save="save" :rowData="rowData"  :editIds="editIds"></xiangqing>
    </Drawer>
    <BaseDialog :visible.sync="selectDelearFlag" :title="'经销商选择'" width="850px" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <DealerList @close="Delearclose" @save="Delearsave"></DealerList>
    </BaseDialog>
    <Drawer :visible.sync="detailsFlag" :title="'发票信息审核详情'" width="60%" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <DetailPage @close="detailClose" @save="detailSave" :rowData="rowData" ></DetailPage>
    </Drawer>
    <Drawer :visible.sync="claimFlag" :title="'索赔单明细'" width="60%" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <ClaimPage @close="claimClose"  :rowData="rowData" ></ClaimPage>
    </Drawer>

  </div>
</template>
<script>
import { exportExcelFile } from '@/utils/index';
import xiangqing from './xiangqing.vue';
import DetailPage from './DetailPage.vue';
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { getListByPage,getListForExport,qryClaimById,exporturl}  from '@/api/claim/factory/ClaimInvoiceCheck/index';
import ClaimPage from './ClaimPage.vue';
import { getAppId } from '@/assets/js/auth';
import { getPartType } from '@/api/dictionary/index';
import { dictionary } from '@/utils/dictMixin';
import moment from "moment";
export default {
  mixins: [dictionary],
  components: {
    xiangqing,
    DealerList,DetailPage,ClaimPage
  },
  data() {
    return {
      dataList: [],
      detailsFlag: false,
      visible: false,
      selectDelearFlag: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      rowData: {},
      editIds: [],
      fetchapi: getListByPage,
      columns: this.creatHeader(),
      fetchParams: { batchStatus:55481002,
        xhrAbort: true}, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false,
      claimFlag:false
    };
  },
  created() {
    if (getAppId() == 'arcfox') {
      this.topFilterInit.find(x => x.fieldName == 'carType').itemList =this.createDictList('5543',[55431001])
    }else{
      this.topFilterInit.find(x => x.fieldName == 'carType').itemList =this.createDictList('5543')
    }
    this.fetchParams.beginSubmitDate = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endSubmitDate = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.chuQi();
  },
  methods: {
    async chuQi() {

      // const res4 = await getPartType({type: 5543});
      // if (res4.resultCode == 200) {
      //
      //   if (getAppId() == 'arcfox') {
      //     delete res4.data[0];
      //   }
      //
      //   this.topFilterInit.find(x => x.fieldName == 'carType').initialValue = '';
      //   this.topFilterInit.find(x => x.fieldName == 'carType').itemList = res4.data.map(y => ({
      //     text: y.codeCnDesc,
      //     value: y.codeId
      //   }));
      // }
    },
    detailClose() {
      this.detailsFlag = false;
    },
    claimClose() {
      this.claimFlag = false;
    },
    detailSave() {
      this.detailsFlag = false;
      this.fetchParams = { ...this.fetchParams };
    },
    Details(val) {
      this.rowData = val;
      this.detailsFlag = true;
    },
    batchExamine() {
      if (this.selectedRows.length > 0) {
        this.selectedRows.map(x => {
          this.editIds.push({batchId: x.batchId, status: 0});
        });
        this.visible = true;
      } else {
        return this.$notify.warning({ title: '提示信息', message: '请先勾选信息！' });
      }

    },
    batchReject() {
      if (this.selectedRows.length > 0) {
        this.selectedRows.map(x => {
          this.editIds.push({batchId: x.batchId, status: 0});
        });
        this.visible = true;
      } else {
        return this.$notify.warning({ title: '提示信息', message: '请先勾选信息！' });
      }
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    close() {
      this.visible = false;
      // this.fetchParams = { ...this.fetchParams };
    },
    chooseOrderNo(val) {
      this.visible = true;
      this.editIds = [];
      this.rowData=val;
    },
    save() {
      this.visible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    // 导出
    async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000144' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginSubmitDate|endSubmitDate").initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginSubmitDate|endSubmitDate").initialValue[1];
      // if(!beginSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '开票开始日期不可为空！' });
      //   return
      // }
      // if(!endSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '开票结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '开票日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '开票日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, { batchStatus:55481002,xhrAbort: false});
      // obj.beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginSubmitDate|endSubmitDate").initialValue[0];
      // obj.endSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginSubmitDate|endSubmitDate").initialValue[1];
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '索赔发票审核导出');
      //   console.log("进去了");
      // });
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //经销商选择页面
    SelectDelear() {
      this.selectDelearFlag = true;
    }, //关闭
    Delearclose() {
      this.selectDelearFlag = false;
    },
    showClaimNum(val) {
      this.rowData = val;
      this.claimFlag = true;
      console.log("!!!!!!!!!!!",this.rowData.batchId);
      // let res = await qryClaimById({id:row.batchId});
    },
    //保存
    Delearsave(obj) {
      this.topFilterInit.map(x => {
        if ('dealerCode' == x.fieldName) {
          x.initialValue = obj[0].dealerCode; //维修工单号返显
        }
      })

      this.selectDelearFlag = false;

    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.beginSubmitDate) {
        this.$notify.warning({ title: '提示信息', message: '开票开始日期不可为空！' });
        return
      }
      if(!val.endSubmitDate) {
        this.$notify.warning({ title: '提示信息', message: '开票结束日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.endSubmitDate).format('YYYY-MM-DD')).diff(moment(val.beginSubmitDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '开票日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.endSubmitDate).format('YYYY-MM-DD')).diff(moment(val.beginSubmitDate), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '开票日期跨度不可超过十二个月！' });
          return
        }
      }
      this.fetchParams = Object.assign({}, this.fetchParams, val,{ batchStatus:55481002,xhrAbort: false});
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请输入经销商代码...',
          initialValue: '',
          // rules: [{ required: true, message: '请选择', trigger: 'blur' }],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.SelectDelear()}/>
              </div>
            );
          }
        },
        {type: 'INPUT', label: '结算单号', fieldName: 'settleNo', placeholder: '请输入结算单号...', initialValue: ''},
        {
          type: 'RANGE_DATE',
          label: '开票时间',
          fieldName: 'beginSubmitDate|endSubmitDate',
          placeholder: '请输入',
          initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        { type: 'SELECT', label: '索赔车辆属性', fieldName: 'carType', placeholder: '请选择索赔车辆属性...', initialValue: '',itemList: [] }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 150,
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" disabled={((props.row.batchStatus == '55481002')) ? false : true} onClick={() => this.chooseOrderNo(props.row)}>
                  审核
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.Details(props.row)}>
                  查看
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.showClaimNum(props.row)}>
                  索赔明细
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '经销商代码', dataIndex: 'dealerCode', sorter: true },
        { title: '经销商名称', dataIndex: 'dealerName', sorter: true },
        {title: '结算批次号', dataIndex: 'batchNo', sorter: true},
        { title: '结算单号', dataIndex: 'settleNo', sorter: true },
        { title: '索赔车辆属性', dataIndex: 'carTypeStr', sorter: true },
        {
          title: '索赔单数',
          dataIndex: 'claimNum',
          sorter: true,
          render: (props) => {
            return <span onClick={() => this.showClaimNum(props.row)} >{ props.row.claimNum }</span>;
          }
        },
        { title: '发票号', dataIndex: 'invoiceNo', sorter: true },
        { title: '发票张数', dataIndex: 'invoiceSheets', sorter: true },
        { title: '结算金额', dataIndex: 'settleSum', sorter: true },
        { title: '考核金额', dataIndex: 'assessSum', sorter: true },
        { title: '索赔金额', dataIndex: 'claimSum', sorter: true },
        { title: '发票金额', dataIndex: 'invoiceMoney', sorter: true },
        { title: '税额', dataIndex: 'invoicePointAmount', sorter: true },
        { title: '开票时间', dataIndex: 'submitDate', sorter: true },
        { title: '邮寄时间', dataIndex: 'sendDate', sorter: true },
        { title: '工时费', dataIndex: 'labourAmount', sorter: true },
        { title: '配件费用', dataIndex: 'partAmount', sorter: true },
        { title: '附加费', dataIndex: 'addAmount', sorter: true },
        { title: '一口价费用', dataIndex: 'priceAmount', sorter: true },
        // { title: '配件缺失索赔', dataIndex: 'peiJianNum', sorter: true },
        // { title: '配件缺失索赔金额', dataIndex: 'peiJianAmount', sorter: true },
        // { title: '让渡索赔', dataIndex: 'rangDuNum', sorter: true },
        // { title: '让渡索赔金额', dataIndex: 'rangDuAmount', sorter: true },
        // { title: '正常索赔', dataIndex: 'zhengChangNum', sorter: true },
        // { title: '正常金额', dataIndex: 'zhengChangAmount', sorter: true },
        // { title: '喷漆索赔', dataIndex: 'penQiNum', sorter: true },
        { title: '快递号', dataIndex: 'expressNo', sorter: true },
        // { title: '快递号', dataIndex: 'batchId', hidden: true }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
